cmake_minimum_required(VERSION 4.0)
project(algo_learning_problems_lists)

set(CMAKE_CXX_STANDARD 17)

# 递归查找所有子目录中的cpp文件
file(GLOB_RECURSE ALL_SOURCE_FILES "*.cpp")

# 创建bin输出目录
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)

# 为每个cpp文件创建单独的可执行文件
foreach(source_file ${ALL_SOURCE_FILES})
    # 生成8位随机码
    string(RANDOM LENGTH 8 ALPHABET "0123456789abcdef" random_code)
    set(target_name "exe_${random_code}")

    # 为每个源文件创建可执行目标
    add_executable(${target_name} ${source_file})

    # 设置输出文件名保持原中文名
    get_filename_component(original_filename ${source_file} NAME)
    get_filename_component(source_dir ${source_file} DIRECTORY)
    file(RELATIVE_PATH relative_dir ${CMAKE_SOURCE_DIR} ${source_dir})

    set_target_properties(${target_name} PROPERTIES
            OUTPUT_NAME "${original_filename}"
            RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}/${relative_dir}"
    )

    message(STATUS "Added executable: ${target_name} from ${source_file}")
    message(STATUS "  Output: ${EXECUTABLE_OUTPUT_PATH}/${relative_dir}/${original_filename}")
endforeach()

# 添加帮助目标
add_custom_target(help_all
        COMMAND ${CMAKE_COMMAND} -E echo "=== 项目构建说明 ==="
        COMMAND ${CMAKE_COMMAND} -E echo "所有可执行文件将保持原中文文件名"
        COMMAND ${CMAKE_COMMAND} -E echo "CMake目标使用随机码名称"
        COMMAND ${CMAKE_COMMAND} -E echo "构建: make"
        COMMAND ${CMAKE_COMMAND} -E echo "运行示例: ./bin/路径/文件名"
)

# 显示找到的所有文件
message(STATUS "Found source files: ${ALL_SOURCE_FILES}")